var productExceptSelf = function(nums) {
  let ans = new Array(nums.length);

  const preArr = new Array(nums.length).fill(1);
  const sufArr = new Array(nums.length).fill(1);

  for (let i = 1; i < nums.length; i++) {
    preArr[i] = preArr[i - 1] * nums[i - 1];
  }

  for (let i = nums.length - 2; i >= 0; i--) {
    sufArr[i] = sufArr[i + 1] * nums[i + 1];
  }

  for (let i = 0; i < nums.length; i++) {
    ans[i] = preArr[i] * sufArr[i];
  }

  console.log(preArr)
  console.log(sufArr)

  return ans;

};

const nums = [1,2,3,4]
console.log(productExceptSelf(nums))